﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Web.Mvc;
using UserNotificationAspMvc.Models;
using System.Text;

namespace UserNotificationAspMvc.HtmlHelpers
{
    public static class DisplayHelper
    {
        public static MvcHtmlString RenderNotificationsList(this HtmlHelper helper,NotificationQueue q)
        {
            if (q != null && q.Notifications != null && q.Notifications.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(@"<div id=""msg-wrapper""></div>");
                sb.Append(@"<script>
                           $(function(){");
                foreach (UserNotification item in q.Notifications.ToList())
                {
                    if (item.Width > 0)
                    {
                        sb.Append(item.ToString());
                    }
                    q.Notifications.Remove(item);
                }
                sb.Append(@"MVCNotificationQueue.Init({ position : '"+q.Position.ToString()+"' })");
                sb.Append(@" });");
                sb.Append("</script>");
                return MvcHtmlString.Create(sb.ToString());
            }
            else
            {
                return MvcHtmlString.Create(string.Empty);
            }
        }

        
        public static MvcHtmlString RenderNotification(this HtmlHelper helper, UserNotification notification)
        {
            NotificationQueue queue = new NotificationQueue();
            if (notification != null && notification.Width > 0)
            {
                queue.Notifications.Add(notification);
            }
            return RenderNotificationsList(helper, queue);
        }

    }
}